﻿using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using WickedFlame.Core;
using WickedFlame.Core.Layers;


namespace WickedFlame.Util.Data
{
    public class VecFileFormat : FileFormat
    {
        public void Export(List<CanvasLayer> surfaces, Stream stream)
        {
            if (surfaces.Count <= 0)
                return;

            var container = new SerializableCanvasContainer(surfaces[0].Width, surfaces[0].Height);
            //foreach (Layer layer in surfaces)
            //{
            //    container.PushLayer(layer);
            //}
            int cnt = surfaces.Count;
            while (cnt >= 0)
            {
                Layer layer = surfaces[cnt];
                container.PushLayer(layer);
                cnt--;
            }

            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, container);
            stream.Close();
        }

        public override void Export(DrawingCanvas canvas, Stream stream)
        {
            var container = new SerializableCanvasContainer(canvas);

            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, container);
            stream.Close();
        }

        public override DrawingCanvas Import(Stream stream)
        {
            SerializableCanvasContainer container;

            var formatter = new BinaryFormatter();
            container = (SerializableCanvasContainer)formatter.Deserialize(stream);
            stream.Close();
            return container.GetCanvas();
        }
    }
}
